package com.javalab._7thread._2Operate._2WaitNotify;

import java.util.LinkedList;
import java.util.Random;

public class MyStack {
    private static final int MAX_SIZE = 5;
    private LinkedList<Integer> list = new LinkedList<>();

    public synchronized void push(Integer element) throws InterruptedException {
        if (list.size() >= MAX_SIZE) {
            wait();
        }
        Thread.sleep(new Random().nextInt(10));
        list.addLast(element);
        System.out.println("push" + list);
        notify();
    }

    public synchronized Integer pop() throws InterruptedException {
        if (list.isEmpty()) {
            wait();
        }
        Thread.sleep(new Random().nextInt(1000));
        Integer result = list.removeFirst();
        System.out.println("pop " + list);
        notify();
        return result;
    }

    public synchronized LinkedList<Integer> list() {
        return list;
    }

    public synchronized int size() {
        return list.size();
    }
}
